package com.sdy.healthcode.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.DateUtil;
import com.sdy.healthcode.biz.constants.ParamConstants;
import com.sdy.healthcode.biz.model.BlackList;
import com.sdy.healthcode.biz.mapper.BlackListMapper;
import com.sdy.healthcode.biz.model.User;
import com.sdy.healthcode.biz.service.BlackListService;
import com.sdy.healthcode.biz.service.SysParamService;
import com.sdy.healthcode.biz.service.UserService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cgj
 * @since 2020-03-09
 */
@Slf4j
@Service
public class BlackListServiceImpl extends BaseServiceImpl<BlackList> implements BlackListService {
    @Autowired
    private BlackListMapper blackListMapper;
    @Autowired
    private BlackListService blackListService;
    @Autowired
    private UserService userService;
    @Autowired
    private SysParamService sysParamService;

    @Override
    public List<BlackList> getBlackList(Integer userId) {
        List<BlackList> list = blackListService.list(new LambdaQueryWrapper<BlackList>()
                .eq(userId != null, BlackList::getUserId, userId)
                .gt(BlackList::getOutBannedTime, new Date()));
        return list;
    }

    @Override
    public Boolean banUser(Integer userId, Integer days, String reason) throws BizException {
        Assert.isNull(userId, "无法获取用户信息");
        User byId = userService.getById(userId);
        Assert.isNull(byId, "无法获取用户信息");
        Date now = new Date();
        Date dateAfterThirtyDays = DateUtil.addDate(now, days);
        boolean save = blackListService.save(new BlackList()
                .setUserId(userId)
                .setPhoneNumber(byId.getPhone())
                .setBannedTime(now)
                .setOutBannedTime(dateAfterThirtyDays)
                .setUserName(byId.getName())
                .setReason(reason));
        return save;
    }

    @Override
    public BlackList userInBlackList(Integer userId) {
        return lambdaQuery()
                .eq(BlackList::getUserId, userId)
                .orderByDesc(BlackList::getId)
                .last("limit 1")
                .one();
    }
}
